package com.ziki.test.socket.protocol;

import java.io.DataInputStream;

import com.ziki.test.socket.data.DeliverData;
import com.ziki.test.socket.data.SubmitData;
import com.ziki.test.socket.util.Log;
import com.ziki.test.socket.util.Maths;
import com.ziki.test.socket.util.PubFun;

/*
 * SMPP 3.4
 */
public class SMPP {
    public SMPP() {
    }

    public int get_LOGIN() {
        if (true) {
            return 0x00000002; // bind_transmitter
        }
        else {
            return 0x00000009; // bind_transceiver
        }
    }

    public int get_LOGIN_RESP() {
        if (true) {
            return 0x80000002;// bind_transmitter_resp
        }
        else {
            return 0x80000009;// bind_transceiver_resp
        }
    }

    public int get_LOGIN3() {
        return 0x00000009; // bind_transceiver
    }

    public int get_LOGIN3_RESP() {
        return 0x80000009;// bind_transceiver_resp
    }

    public int get_LOGIN2() {
        return 0x00000001;
    }

    public int get_LOGIN2_RESP() {
        return 0x80000001;
    }

    public int get_SUBMIT() {
        return 0x00000004;
    }

    public int get_SUBMIT_RESP() {
        return 0x80000004;
    }

    public int get_DELIVER() {
        return 0x00000005;
    }

    public int get_DELIVER_RESP() {
        return 0x80000005;
    }

    public int get_ACTIVE_TEST() {
        return 0x00000015;
    }

    public int get_ACTIVE_TEST_RESP() {
        return 0x80000015;
    }

    public int get_EXIT() {
        return 0X00000006;
    }

    public int get_EXIT_RESP() {
        return 0x80000006;
    }

    // loginType 0:收发，1:发送，2:接收
    public byte[] getLogin(int loginType) throws Exception {
        String system_id = "SendUser";
        String password = "SendPass";
        if (loginType == 2) {
            system_id = "RecvUser";
            password = "RecvPass";
        }
        byte[] loginUser = PubFun.string2byte(system_id);
        byte[] loginPass = PubFun.string2byte(password);

        // system_type、interface_version、ton、npi、address_range
        byte system_type = 0;
        byte interface_version = 0x33; // smpp3.3
        byte ton = 0;
        byte npi = 0;

        String address_range = "";
        byte[] bt_address_range = PubFun.string2byte(address_range);

        int totalSize = 16 + loginUser.length + loginPass.length + 4 + bt_address_range.length;

        int commandID = 0;
        if (loginType == 0 || loginType == 1) {
            commandID = this.get_LOGIN();
        }
        else {
            commandID = this.get_LOGIN2();
        }
        byte[] head = getHead(totalSize, commandID, null);

        byte data[] = new byte[totalSize];
        int location = 0;
        System.arraycopy(head, 0, data, location, head.length);
        location += head.length;
        System.arraycopy(loginUser, 0, data, location, loginUser.length);
        location += loginUser.length;
        System.arraycopy(loginPass, 0, data, location, loginPass.length);
        location += loginPass.length;

        data[location++] = system_type;
        data[location++] = interface_version;
        data[location++] = ton;
        data[location++] = npi;
        System.arraycopy(bt_address_range, 0, data, location, bt_address_range.length);

        StringBuffer str = new StringBuffer();
        str.append("\r\n");
        str.append("system_id：" + system_id);
        str.append("\r\n");
        str.append("password：" + password);
        str.append("\r\n");
        str.append("system_type：" + system_type);
        str.append("\r\n");
        str.append("interface_version：" + interface_version);
        str.append("\r\n");
        str.append("ton：" + ton);
        str.append("\r\n");
        str.append("npi：" + npi);
        str.append("\r\n");
        str.append("address_range：" + address_range);
        str.append("\r\n");

        return data;
    }

    public byte[] getExitResp(byte[] seq) throws Exception {
        int totalSize = 16;
        byte[] head = getHeadResp(totalSize, this.get_EXIT_RESP(), seq);
        return head;
    }

    public byte[] getExit() throws Exception {
        int totalSize = 16;
        byte[] head = getHead(totalSize, this.get_EXIT(), null);
        return head;
    }

    public byte[] getActiveTest() throws Exception {
        int totalSize = 16;
        byte[] head = getHead(totalSize, this.get_ACTIVE_TEST(), null);
        return head;
    }

    public byte[] getActiveTestResp(byte[] seq) throws Exception {
        int totalSize = 16;
        byte[] head = getHeadResp(totalSize, this.get_ACTIVE_TEST_RESP(), seq);
        return head;
    }

    public byte[] getDeliverResp(byte[] seq, byte[] msgID) throws Exception {
        int totalSize = 24;
        int location = 0;
        byte data[] = new byte[totalSize];
        byte[] head = getHeadResp(totalSize, this.get_DELIVER_RESP(), seq);

        for (location = 0; location < head.length; location++) {
            data[location] = head[location];
        }
        PubFun.writeString("", data, 9, location);
        location += 9;
        return data;
    }

    public byte[] getSubmit(SubmitData submitData) throws Exception {
        byte[] service_type = PubFun.string2byte(submitData.FServiceCode);
        // byte[] service_type = new byte[1];
        // service_type[0] = 0;

        byte source_address_ton = 0;
        byte source_address_npi = 0;
        byte[] source_address = PubFun.string2byte(submitData.FSPnumber);
        byte dest_address_ton = 0;
        byte dest_address_npi = 0;
        byte[] destination_address = PubFun.string2byte(submitData.FMobile);
        byte esm_class = 0;
        byte protocol_id = 0;
        byte priority_flag = submitData.FPri;
        byte schedule_delivery_time = 0;
        byte validity_peroid = 0;
        byte registered_delivery_flag = submitData.FReportFlag;
        byte replace_if_present_flag = 0;
        byte data_coding = submitData.FDCS;
        byte sm_default_msg_id = 0;
        byte[] btContent = null;
        switch (data_coding) {
        case 8:
            btContent = submitData.FContent.getBytes("UTF-16BE");
            break;
        case 15:
            btContent = submitData.FContent.getBytes("gb2312");
            break;
        default:
            btContent = submitData.FContent.getBytes();
            break;
        }
        // int sm_length = btContent.length + 1;
        int sm_length = btContent.length;

        int totalSize = 16 + service_type.length + 2 + source_address.length + 2 + destination_address.length + 10
                + btContent.length + 1;
        byte[] data = new byte[totalSize];
        int loc = 0;
        byte[] head = getHead(totalSize, this.get_SUBMIT(), submitData);
        System.arraycopy(head, 0, data, loc, head.length);
        loc += head.length;

        // body
        System.arraycopy(service_type, 0, data, loc, service_type.length);
        loc += service_type.length;
        data[loc++] = source_address_ton;
        data[loc++] = source_address_npi;
        System.arraycopy(source_address, 0, data, loc, source_address.length);
        loc += source_address.length;
        data[loc++] = dest_address_ton;
        data[loc++] = dest_address_npi;
        System.arraycopy(destination_address, 0, data, loc, destination_address.length);
        loc += destination_address.length;
        data[loc++] = esm_class;
        data[loc++] = protocol_id;
        data[loc++] = priority_flag;
        data[loc++] = schedule_delivery_time;
        data[loc++] = validity_peroid;
        data[loc++] = registered_delivery_flag;
        data[loc++] = replace_if_present_flag;
        data[loc++] = data_coding;
        data[loc++] = sm_default_msg_id;
        data[loc++] = (byte) sm_length;
        System.arraycopy(btContent, 0, data, loc, btContent.length);
        loc += btContent.length;
        data[loc++] = 0;

        StringBuffer sb = new StringBuffer();
        sb.append("service_type:" + submitData.FServiceCode + "\r\n");
        sb.append("source_address_ton:" + source_address_ton + "\r\n");
        sb.append("source_address_npi:" + source_address_npi + "\r\n");
        sb.append("source_address:" + submitData.FSPnumber + "\r\n");
        sb.append("dest_address_ton:" + dest_address_ton + "\r\n");
        sb.append("dest_address_npi:" + dest_address_npi + "\r\n");
        sb.append("destination_address:" + submitData.FMobile + "\r\n");
        sb.append("esm_class:" + esm_class + "\r\n");
        sb.append("protocol_id:" + protocol_id + "\r\n");
        sb.append("priority_flag:" + priority_flag + "\r\n");
        sb.append("schedule_delivery_time:" + schedule_delivery_time + "\r\n");
        sb.append("validity_peroid:" + validity_peroid + "\r\n");
        sb.append("registered_delivery_flag:" + submitData.FReportFlag + "\r\n");
        sb.append("replace_if_present_flag:" + replace_if_present_flag + "\r\n");
        sb.append("data_coding:" + submitData.FDCS + "\r\n");
        sb.append("sm_default_msg_id:" + sm_default_msg_id + "\r\n");
        sb.append("sm_length:" + sm_length + "\r\n");
        sb.append("short_message_text:" + submitData.FContent + "\r\n");

        return data;
    }

    public byte[] getDeliver(DeliverData deliverData) throws Exception {
        byte[] service_type = PubFun.string2byte(deliverData.serviceCode);
        // byte[] service_type = new byte[1];
        // service_type[0] = 0;

        byte source_address_ton = 0;
        byte source_address_npi = 0;
        byte[] source_address = PubFun.string2byte(deliverData.mobile);
        byte dest_address_ton = 0;
        byte dest_address_npi = 0;
        byte[] destination_address = PubFun.string2byte(deliverData.spNumber);
        byte esm_class = 0;
        byte protocol_id = 0;
        byte priority_flag = 0;
        byte schedule_delivery_time = 0;
        byte validity_peroid = 0;
        byte registered_delivery_flag = 0;
        byte replace_if_present_flag = 0;
        byte data_coding = deliverData.coding;
        byte sm_default_msg_id = 0;
        byte[] btContent = null;
        switch (data_coding) {
        case 8:
            btContent = deliverData.content.getBytes("UTF-16BE");
            break;
        case 15:
            btContent = deliverData.content.getBytes("gb2312");
            break;
        default:
            btContent = deliverData.content.getBytes();
            break;
        }
        // int sm_length = btContent.length + 1;
        int sm_length = btContent.length;

        int totalSize = 16 + service_type.length + 2 + source_address.length + 2 + destination_address.length + 10
                + btContent.length + 1;
        byte[] data = new byte[totalSize];
        int loc = 0;
        byte[] head = getHead(totalSize, this.get_DELIVER(), null);
        System.arraycopy(head, 0, data, loc, head.length);
        loc += head.length;

        // body
        System.arraycopy(service_type, 0, data, loc, service_type.length);
        loc += service_type.length;
        data[loc++] = source_address_ton;
        data[loc++] = source_address_npi;
        System.arraycopy(source_address, 0, data, loc, source_address.length);
        loc += source_address.length;
        data[loc++] = dest_address_ton;
        data[loc++] = dest_address_npi;
        System.arraycopy(destination_address, 0, data, loc, destination_address.length);
        loc += destination_address.length;
        data[loc++] = esm_class;
        data[loc++] = protocol_id;
        data[loc++] = priority_flag;
        data[loc++] = schedule_delivery_time;
        data[loc++] = validity_peroid;
        data[loc++] = registered_delivery_flag;
        data[loc++] = replace_if_present_flag;
        data[loc++] = data_coding;
        data[loc++] = sm_default_msg_id;
        data[loc++] = (byte) sm_length;
        System.arraycopy(btContent, 0, data, loc, btContent.length);
        loc += btContent.length;
        data[loc++] = 0;

        StringBuffer sb = new StringBuffer();
        sb.append("service_type:" + deliverData.serviceCode + "\r\n");
        sb.append("source_address_ton:" + source_address_ton + "\r\n");
        sb.append("source_address_npi:" + source_address_npi + "\r\n");
        sb.append("source_address:" + deliverData.spNumber + "\r\n");
        sb.append("dest_address_ton:" + dest_address_ton + "\r\n");
        sb.append("dest_address_npi:" + dest_address_npi + "\r\n");
        sb.append("destination_address:" + deliverData.mobile + "\r\n");
        sb.append("esm_class:" + esm_class + "\r\n");
        sb.append("protocol_id:" + protocol_id + "\r\n");
        sb.append("priority_flag:" + priority_flag + "\r\n");
        sb.append("schedule_delivery_time:" + schedule_delivery_time + "\r\n");
        sb.append("validity_peroid:" + validity_peroid + "\r\n");
        sb.append("registered_delivery_flag:" + registered_delivery_flag + "\r\n");
        sb.append("replace_if_present_flag:" + replace_if_present_flag + "\r\n");
        sb.append("data_coding:" + deliverData.coding + "\r\n");
        sb.append("sm_default_msg_id:" + sm_default_msg_id + "\r\n");
        sb.append("sm_length:" + sm_length + "\r\n");
        sb.append("short_message_text:" + deliverData.content + "\r\n");

        return data;
    }

    // 获取包头
    public byte[] getHead(int totalSize, int commandID, SubmitData submitData) throws Exception {
        byte data[] = new byte[16];
        // 序列号
        int location = 0;
        int seq = PubFun.getSeq();
        byte tmp[] = PubFun.toByteArray(totalSize);
        for (int i = 0; i < tmp.length; i++) {
            data[location++] = tmp[i];
        }
        tmp = PubFun.toByteArray(commandID);
        for (int i = 0; i < tmp.length; i++) {
            data[location++] = tmp[i];
        }
        int commandStatus = 0;
        tmp = PubFun.toByteArray(commandStatus);
        for (int i = 0; i < tmp.length; i++) {
            data[location++] = tmp[i];
        }
        tmp = PubFun.toByteArray(seq);
        for (int i = 0; i < tmp.length; i++) {
            data[location++] = tmp[i];
        }
        if (submitData != null) {
            System.arraycopy(tmp, 0, submitData.seqID, 0, tmp.length);
        }
        return data;
    }

    public byte[] getHeadResp(int totalSize, int commandID, byte[] seq) throws Exception {
        byte data[] = new byte[16];
        int location = 0;
        byte tmp[] = PubFun.toByteArray(totalSize);
        for (int i = 0; i < tmp.length; i++) {
            data[location++] = tmp[i];
        }
        tmp = PubFun.toByteArray(commandID);
        for (int i = 0; i < tmp.length; i++) {
            data[location++] = tmp[i];
        }
        int commandStatus = 0;
        tmp = PubFun.toByteArray(commandStatus);
        for (int i = 0; i < tmp.length; i++) {
            data[location++] = tmp[i];
        }
        for (int i = 0; i < 4; i++) { // 实际只有4个字节
            data[location++] = seq[i];
        }
        return data;
    }

    public byte[] getSendData(int commandID, int commandStatus, byte[] seq, byte[] bodyDate) throws Exception {
        int totalSize = bodyDate.length + 1 + 16;
        byte data[] = new byte[totalSize];
        int location = 0;
        byte tmp[] = PubFun.toByteArray(totalSize);
        for (int i = 0; i < 4; i++) {// 实际只有4个字节
            data[location++] = tmp[i];
        }
        tmp = PubFun.toByteArray(commandID);
        for (int i = 0; i < 4; i++) {// 实际只有4个字节
            data[location++] = tmp[i];
        }
        // int commandStatus = 0;
        tmp = PubFun.toByteArray(commandStatus);
        for (int i = 0; i < 4; i++) {// 实际只有4个字节
            data[location++] = tmp[i];
        }
        for (int i = 0; i < 4; i++) { // 实际只有4个字节
            data[location++] = seq[i];
        }
        for (int i = 0; i < bodyDate.length; i++) {
            data[location++] = bodyDate[i];
        }
        return data;
    }

    /**
     * 虚拟
     */
    public int parseHead(DataInputStream dis, byte[] seqID, int[] commandIDStatus) throws Exception {
        return 0;
    }

    /**
     * 分析数据头
     * 
     * @param data 收到的数据
     * @param seqID 返回数据seqID
     * @param commandIDStatus 返回命令ID和状态
     * @return 命令类型commandID
     * @throws Exception
     */
    public int parseHead(byte[] data, byte[] seqID, int[] commandIDStatus) throws Exception {
        byte[] command_id = new byte[4];
        byte[] command_status = new byte[4];
        byte[] sequence_no = new byte[4];
        System.arraycopy(data, 4, command_id, 0, command_id.length);
        System.arraycopy(data, 8, command_status, 0, command_status.length);
        System.arraycopy(data, 12, sequence_no, 0, sequence_no.length);
        System.arraycopy(sequence_no, 0, seqID, 0, seqID.length);

        int commandID = PubFun.bytes2int(command_id);
        int commandStatus = PubFun.bytes2int(command_status);
        commandIDStatus[0] = commandID;
        commandIDStatus[1] = commandStatus;
        return commandID;
    }

    public void parseBodyLoginResp(DataInputStream dis, int status) throws Exception {
    }

    /**
     * 分析BIND状态数据??<br>
     * 主要功能是返回成功就设置mySocket连接成功loginFlag
     * 
     * @param data
     * @param mySocket
     * @param status
     * @throws Exception
     */
    public void parseBodyLoginResp(byte[] data, int status) throws Exception {
        int loc = 16;
        String[] str = new String[2];
        str = PubFun.getStringEnd(data, loc, 16);
        String system_id = str[0];
        loc = Maths.atoi(str[1]);
        StringBuffer sb = new StringBuffer();
        sb.append("Status：");
        sb.append(status);
        sb.append("，System_id：");
        sb.append(system_id);
        byte[] command_id = new byte[4];
        System.arraycopy(data, 4, command_id, 0, command_id.length);
        int commandID = PubFun.bytes2int(command_id);
    }

    public void parseBodySubmitResp(DataInputStream dis, SubmitData submitData) throws Exception {
    }

    /**
     * 分析发送状态包的数据,取出返回的msgID赋给submitData
     * 
     * @param data
     * @param submitData
     * @throws Exception
     */
    public void parseBodySubmitResp(byte[] data, SubmitData submitData) throws Exception {
        byte[] command_length = new byte[4];
        System.arraycopy(data, 0, command_length, 0, command_length.length);
        int totalSize = PubFun.bytes2int(command_length);
        System.arraycopy(data, 16, submitData.msgID, 0, totalSize - 16 - 1);
    }

    public void parseBodyDeliver(DataInputStream dis, DeliverData data) throws Exception {
    }

    /**
     * 分析Deliver包数据,给deliverData进行赋值
     * 
     * @param data
     * @param deliverData
     * @throws Exception
     */
    public void parseBodyDeliver(byte[] data, DeliverData deliverData) throws Exception {
        int loc = 16;
        String[] str = new String[2];
        str = PubFun.getStringEnd(data, loc, 6);
        deliverData.serviceCode = str[0];
        loc = Maths.atoi(str[1]);

        int source_address_ton = data[loc++];
        int source_address_npi = data[loc++];
        str = PubFun.getStringEnd(data, loc, 21);
        deliverData.mobile = str[0];
        loc = Maths.atoi(str[1]);

        int dest_address_ton = data[loc++];
        int dest_address_npi = data[loc++];
        str = PubFun.getStringEnd(data, loc, 21);
        deliverData.spNumber = str[0];
        loc = Maths.atoi(str[1]);

        byte esm_class = data[loc++];
        String str_esm_class = Integer.toBinaryString(esm_class);
        str_esm_class = PubFun.padLeading(str_esm_class, 8, "0");
        deliverData.deliverType = Integer.parseInt(str_esm_class.substring(5, 6));

        int protocol_id = data[loc++];
        int priority_flag = data[loc++];

        str = PubFun.getStringEnd(data, loc, 17);
        String schedule_delivery_time = str[0];
        loc = Maths.atoi(str[1]);

        str = PubFun.getStringEnd(data, loc, 17);
        String validity_peroid = str[0];
        loc = Maths.atoi(str[1]);

        byte registered_delivery_flag = data[loc++];

        int replace_if_present_flag = data[loc++];

        deliverData.coding = data[loc++];

        int sm_default_msg_id = data[loc++] & 0xff;

        int sm_length = data[loc++] & 0xff;

        byte[] b = new byte[sm_length];
        System.arraycopy(data, loc, b, 0, b.length);
        switch (deliverData.coding) {
        case 0:
            deliverData.content = new String(b);
            break;
        case 8:
            deliverData.content = new String(b, "UTF-16BE");
            break;
        case 15:
            deliverData.content = new String(b, "GB2312");
            break;
        default:
            // deliverData.content = PubFun.convertBytesToString(b);
            deliverData.content = new String(b);
            break;
        }

        StringBuffer sb = new StringBuffer();
        sb.append("\r\n");
        sb.append("service_type：");
        sb.append(deliverData.serviceCode);
        sb.append("\r\n");
        sb.append("source_address_ton：");
        sb.append(source_address_ton);
        sb.append("\r\n");
        sb.append("source_address_npi：");
        sb.append(source_address_npi);
        sb.append("\r\n");
        sb.append("source_address：");
        sb.append(deliverData.mobile);
        sb.append("\r\n");
        sb.append("dest_address_ton：");
        sb.append(dest_address_ton);
        sb.append("\r\n");
        sb.append("dest_address_npi：");
        sb.append(dest_address_npi);
        sb.append("\r\n");
        sb.append("destination_address：");
        sb.append(deliverData.spNumber);
        sb.append("\r\n");
        sb.append("esm_class：");
        sb.append(esm_class);
        sb.append("\r\n");
        sb.append("protocol_ID：");
        sb.append(protocol_id);
        sb.append("\r\n");
        sb.append("priority_flag：");
        sb.append(priority_flag);
        sb.append("\r\n");
        sb.append("schedule_delivery_time：");
        sb.append(schedule_delivery_time);
        sb.append("\r\n");
        sb.append("validity_peroid：");
        sb.append(validity_peroid);
        sb.append("\r\n");
        sb.append("registered_delivery_flag：");
        sb.append(registered_delivery_flag);
        sb.append("\r\n");
        sb.append("replace_if_present_flag：");
        sb.append(replace_if_present_flag);
        sb.append("\r\n");
        sb.append("data_coding：");
        sb.append(deliverData.coding);
        sb.append("\r\n");
        sb.append("sm_default_msg_id：");
        sb.append(sm_default_msg_id);
        sb.append("\r\n");
        sb.append("sm_length：");
        sb.append(sm_length);
        sb.append("\r\n");
        sb.append("short_message_text：");
        sb.append(deliverData.content);
        sb.append("\r\n");
    }

    /**
     * 分析Deliver包数据,给deliverData进行赋值
     * 
     * @param data
     * @param deliverData
     * @throws Exception
     */
    public byte[] parseDeliverStatus(byte[] data, String msgid) {
        try {
            SubmitData submit = new SubmitData();
            int loc = 16;
            String[] str = new String[2];
            str = PubFun.getStringEnd(data, loc, 6);
            // deliverData.serviceCode = str[0];
            submit.FServiceCode = str[0];
            loc = Maths.atoi(str[1]);

            byte source_address_ton = data[loc++];
            byte source_address_npi = data[loc++];
            str = PubFun.getStringEnd(data, loc, 21);
            // deliverData.mobile = str[0];
            submit.FSPnumber = str[0];
            loc = Maths.atoi(str[1]);

            byte dest_address_ton = data[loc++];
            byte dest_address_npi = data[loc++];
            str = PubFun.getStringEnd(data, loc, 21);
            // deliverData.spNumber = str[0];
            submit.FMobile = str[0];
            loc = Maths.atoi(str[1]);

            byte esm_class = data[loc++];
            String str_esm_class = Integer.toBinaryString(esm_class);
            str_esm_class = PubFun.padLeading(str_esm_class, 8, "0");
            submit.FReportFlag = esm_class;
            // deliverData.deliverType = Integer.parseInt(str_esm_class.substring(5,
            // 6));

            byte protocol_id = data[loc++];
            byte priority_flag = data[loc++];

            str = PubFun.getStringEnd(data, loc, 17);
            String schedule_delivery_time = str[0];
            loc = Maths.atoi(str[1]);

            str = PubFun.getStringEnd(data, loc, 17);
            String validity_peroid = str[0];
            loc = Maths.atoi(str[1]);

            byte registered_delivery_flag = data[loc++];

            byte replace_if_present_flag = data[loc++];

            // deliverData.coding = data[loc++];
            submit.FDCS = data[loc++];

            int sm_default_msg_id = data[loc++] & 0xff;

            int sm_length = data[loc++] & 0xff;

            byte[] b = new byte[sm_length];
            System.arraycopy(data, loc, b, 0, b.length);
            switch (submit.FDCS) {
            case 0:
                submit.FContent = new String(b);
                break;
            case 8:
                submit.FContent = new String(b, "UTF-16BE");
                break;
            case 15:
                submit.FContent = new String(b, "GB2312");
                break;
            default:
                // deliverData.content = PubFun.convertBytesToString(b);
                submit.FContent = new String(b);
                break;
            }

            StringBuffer sb = new StringBuffer();
            sb.append("service_type：");
            sb.append(submit.FServiceCode);
            sb.append("\r\n");
            sb.append("source_address_ton：");
            sb.append(source_address_ton);
            sb.append("\r\n");
            sb.append("source_address_npi：");
            sb.append(source_address_npi);
            sb.append("\r\n");
            sb.append("source_address：");
            sb.append(submit.FSPnumber);
            sb.append("\r\n");
            sb.append("dest_address_ton：");
            sb.append(dest_address_ton);
            sb.append("\r\n");
            sb.append("dest_address_npi：");
            sb.append(dest_address_npi);
            sb.append("\r\n");
            sb.append("destination_address：");
            sb.append(submit.FMobile);
            sb.append("\r\n");
            sb.append("esm_class：");
            sb.append(esm_class);
            sb.append("\r\n");
            sb.append("protocol_ID：");
            sb.append(protocol_id);
            sb.append("\r\n");
            sb.append("priority_flag：");
            sb.append(priority_flag);
            sb.append("\r\n");
            sb.append("schedule_delivery_time：");
            sb.append(schedule_delivery_time);
            sb.append("\r\n");
            sb.append("validity_peroid：");
            sb.append(validity_peroid);
            sb.append("\r\n");
            sb.append("registered_delivery_flag：");
            sb.append(registered_delivery_flag);
            sb.append("\r\n");
            sb.append("replace_if_present_flag：");
            sb.append(replace_if_present_flag);
            sb.append("\r\n");
            sb.append("data_coding：");
            sb.append(submit.FDCS);
            sb.append("\r\n");
            sb.append("sm_default_msg_id：");
            sb.append(sm_default_msg_id);
            sb.append("\r\n");
            sb.append("sm_length：");
            sb.append(sm_length);
            sb.append("\r\n");
            sb.append("short_message_text：");
            sb.append(submit.FContent);
            sb.append("\r\n");
            // System.err.println(sb.toString());
            // Log.writeLog("接收数据", sb.toString());

            if (registered_delivery_flag == 1) {
                byte[] service_type = PubFun.string2byte("OTA");
                source_address_ton = 0;
                source_address_npi = 0;
                byte[] source_address = PubFun.string2byte(submit.FMobile);
                dest_address_ton = 0;
                dest_address_npi = 0;
                byte[] destination_address = PubFun.string2byte(submit.FSPnumber);
                esm_class = 0;
                protocol_id = 0;
                priority_flag = submit.FPri;
                registered_delivery_flag = priority_flag;
                replace_if_present_flag = 0;
                int data_coding = 0;
                sm_default_msg_id = 0;
                byte[] btContent = null;

                String msg = "";
                msg += "id:" + msgid + " ";
                msg += "sub:1 ";
                msg += "dlvrd:1 ";
                msg += "submit date:0 ";
                msg += "done date:0 ";
                msg += "stat:0 ";
                msg += "err:0 ";
                String shortMessage = submit.FContent;
                int msgLen = shortMessage.length();
                msg += "text:" + shortMessage.substring(0, (msgLen > 10 ? 10 : msgLen));
                if (msg.length() > 100)
                    msg = msg.substring(0, 100);
                switch (data_coding) {
                case 8:
                    btContent = msg.getBytes("UTF-16BE");
                    break;
                case 15:
                    btContent = msg.getBytes("gb2312");
                    break;
                default:
                    btContent = msg.getBytes();
                    break;
                }
                // int sm_length = btContent.length + 1;
                sm_length = btContent.length;

                int totalSize = 16 + service_type.length + 2 + source_address.length + 2 + destination_address.length + 10
                        + sm_length;
                byte[] rtndata = new byte[totalSize];
                loc = 0;
                byte[] head = getHead(totalSize, this.get_DELIVER(), null);
                System.arraycopy(head, 0, rtndata, loc, head.length);
                loc += head.length;

                // body
                System.arraycopy(service_type, 0, rtndata, loc, service_type.length);
                loc += service_type.length;
                rtndata[loc++] = source_address_ton;
                rtndata[loc++] = source_address_npi;
                System.arraycopy(source_address, 0, rtndata, loc, source_address.length);
                loc += source_address.length;
                rtndata[loc++] = dest_address_ton;
                rtndata[loc++] = dest_address_npi;
                System.arraycopy(destination_address, 0, rtndata, loc, destination_address.length);
                loc += destination_address.length;
                rtndata[loc++] = 0x04;
                rtndata[loc++] = protocol_id;
                rtndata[loc++] = priority_flag;
                rtndata[loc++] = 0;
                rtndata[loc++] = 0;
                rtndata[loc++] = registered_delivery_flag;
                rtndata[loc++] = replace_if_present_flag;
                rtndata[loc++] = 0;
                rtndata[loc++] = 0;
                rtndata[loc++] = (byte) sm_length;
                System.arraycopy(btContent, 0, rtndata, loc, btContent.length);
                loc += btContent.length;
                System.out.println("=========data length: " + rtndata.length);
                System.out.println("=========msg: " + msg);
                System.out.println("=========msg.length: " + msg.length());
                return rtndata;
            }
        } catch (Exception e) {
            Log.writeLog("Error", e.toString());
            e.printStackTrace();
        }
        return null;
    }

    public String getCommandStr(int commandID) {
        String commandStr = "";
        if (commandID == this.get_ACTIVE_TEST()) {
            commandStr = "ENQUIRE_LINK";
        }
        else if (commandID == this.get_ACTIVE_TEST_RESP()) {
            commandStr = "ENQUIRE_LINK_RESP";
        }
        else if (commandID == this.get_DELIVER()) {
            commandStr = "DELIVER_SM";
        }
        else if (commandID == this.get_DELIVER_RESP()) {
            commandStr = "DELIVER_SM_RESP";
        }
        else if (commandID == this.get_EXIT()) {
            commandStr = "UNBIND";
        }
        else if (commandID == this.get_EXIT_RESP()) {
            commandStr = "UNBIND_RESP";
        }
        else if (commandID == this.get_LOGIN()) {
            if (true) {
                commandStr = "BIND_TRANSMITTER";
            }
            else {
                commandStr = "BIND_TRANSCEIVER";
            }
        }
        else if (commandID == this.get_LOGIN_RESP()) {
            if (true) {
                commandStr = "BIND_TRANSMITTER_RESP";
            }
            else {
                commandStr = "BIND_TRANSCEIVER_RESP";
            }
        }
        else if (commandID == this.get_LOGIN2()) {
            commandStr = "BIND_RECEIVER";
        }
        else if (commandID == this.get_LOGIN2_RESP()) {
            commandStr = "BIND_RECEIVER_RESP";
        }
        else if (commandID == this.get_SUBMIT()) {
            commandStr = "SUBMIT_SM";
        }
        else if (commandID == this.get_SUBMIT_RESP()) {
            commandStr = "SUBMIT_SM_RESP";
        }
        return commandStr;
    }

}
